/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gui;

import gui.UsersListGui;

import java.util.Calendar;
import java.awt.GridLayout;
import javax.swing.border.TitledBorder;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import msg.MsgHistoryBack;
/**
 *
 * @author tomek
 */
public class HistoryRequestGui extends JFrame implements ActionListener {
    
    private client.HistoryRequest myHistory;
    private JTextField[] tfFrom, tfTo;
    private JPanel mainPanel, pFrom, pTo;
    private JComboBox combo;
    private JButton requestButton;
    
    private final String[] labels = {"Rok", "Miesiac", "Dzien", "Godzina", "Minuta"};
    private final String[] choices = {"wybierz...", "Ostatnia godzina", "Ostatnie 24h", "Ostatni tydzien", "Ostatni miesiac"};
    
    public HistoryRequestGui(client.HistoryRequest h) {
        super("Historia");
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setIconImage(UsersListGui.createIcon(UsersListGui.Menu.HISTORY.imgURL()).getImage());
        
        myHistory = h;
        combo = new JComboBox(choices);
        combo.addActionListener(this);
        
        requestButton = new JButton("Pobierz historie");
        requestButton.setActionCommand("pobierz");
        requestButton.addActionListener(this);
        
        tfFrom = new JTextField[5];
        tfTo = new JTextField[5];
        mainPanel = new JPanel();
        pFrom = new JPanel(new GridLayout(5,2));
        pTo = new JPanel(new GridLayout(5,2));
        
        pFrom.setBorder(new TitledBorder("Data od:"));
        pTo.setBorder(new TitledBorder("Data do:"));
        
        
        for(int i = 0; i < 5; i++) {
            tfFrom[i] = new JTextField(4);
            tfTo[i] = new JTextField(4);
            
            pFrom.add(new JLabel(labels[i]));
            //tfFrom[i].setBorder(new TitledBorder(labels[i]));
            pFrom.add(tfFrom[i]);
            
            pTo.add(new JLabel(labels[i]));
            //tfFrom[i].setBorder(new TitledBorder(labels[i]));
            pTo.add(tfTo[i]); 
        }
        
        add(mainPanel);
        mainPanel.add(pFrom);
        mainPanel.add(pTo);
        mainPanel.add(combo);
        mainPanel.add(requestButton);
        
        setResizable(false);
    }
    
    public void showGui() {
        pack();
        super.setVisible(true);
    }
    
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("pobierz")) {
            try {
                //wysyła żądanie pobraniia historii z serwera
                MsgHistoryBack historyBack = myHistory.sendHistoryRequest(getDateFrom(), getDateTo());

                if(historyBack != null) {
                    new client.HistoryDisplay(historyBack);
                }
                else JOptionPane.showMessageDialog(new JFrame(), "Nie udalo sie pobrac histori.", "Blad", JOptionPane.ERROR_MESSAGE);
                
            }
            catch (NumberFormatException exc) {
                JOptionPane.showMessageDialog(new JFrame(), "Zle paramety!", "Blad", JOptionPane.ERROR_MESSAGE);
            }
            catch (IndexOutOfBoundsException exc) {
                exc.printStackTrace();
                JOptionPane.showMessageDialog(new JFrame(), "Blad podczas pobierania!", "Blad", JOptionPane.ERROR_MESSAGE);
            }
        }
        else {
            JComboBox cb = (JComboBox)e.getSource();
            String choice = (String)cb.getSelectedItem();
            if(choice.equals(choices[1])) {
                Calendar cal = Calendar.getInstance();
                setDateFrom(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.HOUR)-1, cal.get(Calendar.MINUTE));
                setDateTo(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.HOUR), cal.get(Calendar.MINUTE));
            }
            else if(choice.equals(choices[2])) {
                Calendar cal = Calendar.getInstance();
                setDateFrom(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH)-1, cal.get(Calendar.HOUR), cal.get(Calendar.MINUTE));
                setDateTo(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.HOUR), cal.get(Calendar.MINUTE));
            }
            else if(choice.equals(choices[3])) {
                Calendar cal = Calendar.getInstance();
                setDateFrom(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH)-7, cal.get(Calendar.HOUR), cal.get(Calendar.MINUTE));
                setDateTo(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.HOUR), cal.get(Calendar.MINUTE));
            }
            else if(choice.equals(choices[4])) {
                Calendar cal = Calendar.getInstance();
                setDateFrom(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)-1, cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.HOUR), cal.get(Calendar.MINUTE));
                setDateTo(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.HOUR), cal.get(Calendar.MINUTE));
            }
        }
    }
    
    public int[] getDateFrom() {
        int[] result = new int[5];
        for(int i = 0; i < 5; i++) {
            result[i] = Integer.parseInt(tfFrom[i].getText());
        }
        return result;
    }
    
    public int[] getDateTo() {
        int[] result = new int[5];
        for(int i = 0; i < 5; i++) {
            result[i] = Integer.parseInt(tfTo[i].getText());
        }
        return result;
    }
    
    public void setDateFrom(int year, int month, int day, int hour, int minute) {
        tfFrom[0].setText(year+"");
        tfFrom[1].setText(month+"");
        tfFrom[2].setText(day+"");
        tfFrom[3].setText(hour+"");
        tfFrom[4].setText(minute+"");
    }
    
    public void setDateTo(int year, int month, int day, int hour, int minute) {
        tfTo[0].setText(year+"");
        tfTo[1].setText(month+"");
        tfTo[2].setText(day+"");
        tfTo[3].setText(hour+"");
        tfTo[4].setText(minute+"");
    }
    

    
}
